Imports System.Xml
Public Class XMLFunctions



    Dim xmldom As New XmlDocument

    Dim aryXPath As New ArrayList

    Dim arylist As New ArrayList

    Public strRootElementName As String


    Public XMLValues As New Hashtable

    Public Sub New(ByVal xmldom As XmlDocument)
        'xmldom.Load("C:\Documents and Settings\adwara\My Documents\Visual Studio 2005\Projects\AJAXEnabledWebApplication2\bin\sendxml.xml")
        strRootElementName = xmldom.FirstChild.Name
        PopulateArrayList(xmldom.FirstChild)
    End Sub

    Public Sub New(ByVal strXML As String)
        xmldom.LoadXml(strXML)
        strRootElementName = xmldom.FirstChild.Name
        PopulateArrayList(xmldom.FirstChild)
    End Sub


    Sub PopulateArrayList(ByVal xmlnode As XmlNode)

        Dim xmlchildnode As XmlNode


        For Each xmlchildnode In xmlnode.ChildNodes

            If xmlchildnode.GetType.Name <> "XmlText" Then
                arylist.Add(xmlchildnode.ParentNode.Name & "," & xmlchildnode.InnerText)

                If xmlchildnode.ChildNodes.Count <= 1 Then
                    Try
                        XMLValues.Add(FormatXpath(xmlchildnode), xmlchildnode.InnerText)
                    Catch ex As Exception
                    End Try
                End If



            End If

            PopulateArrayList(xmlchildnode)

        Next
    End Sub


    Function FormatXpath(ByVal xmlnode As XmlNode) As String

        Dim strpath As String = ""
        Dim strSeed As String

        Dim strXPath As String = ""
        Dim blnEntry As Boolean = True


        strSeed = xmlnode.Name



        While strSeed <> strRootElementName
            strSeed = GetParent(strSeed, xmlnode.ParentNode.Name, blnEntry)
            strpath = strSeed + "/" + strpath

            blnEntry = False
        End While
        strXPath = strpath + xmlnode.Name
        'aryXpaths.Add(strXPath & objfield.Name)

        Return strXPath

    End Function


    Function GetParent(ByVal strChild As String, ByVal strDeclaringType As String, ByVal blnEntry As Boolean) As String
        Dim i As Integer
        Dim str() As String
        For i = 0 To arylist.Count - 1
            str = Split(arylist(i), ",")

            If blnEntry Then
                If str(0) = strDeclaringType And str(1) = strChild Then
                    Return str(0)
                End If
            Else
                If str(1) = strChild Then
                    Return str(0)
                End If
            End If

        Next
        Return ""
    End Function
End Class
